package Persistencia;

import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import java.net.PasswordAuthentication;

public class conexao
{
    //216.18.20.210
    //stocker
    //YTFLFzFyMUjpSyzt
    private static final String DRIVER = "com.mysql.jdbc.Driver";
    private static final String STR_CON = "jdbc:mysql://localhost/stocker";  
    private static final String USER = "root";  
    private static final String PASSWORD = "";  
    private Connection conexao;
    public Statement statement;
    public ResultSet resultset;
    public boolean status;

    public boolean conecta()
    {
        boolean result = true;
        try
        {
            Class.forName(DRIVER);
            conexao = DriverManager.getConnection(STR_CON, USER, PASSWORD);
            //JOptionPane.showMessageDialog(null, "Conectou");
        }
        catch (ClassNotFoundException erro)
        {
            JOptionPane.showMessageDialog (null, "Erro: "+erro);
            result = false;
        }
        catch (SQLException erro)
        {
            JOptionPane.showMessageDialog (null, "Erro: "+erro);
            result = false;
        }
        return result;
    }

    public void desconectar ()
    {
        try {
            conexao.close();
        } catch (SQLException erro) {
            JOptionPane.showMessageDialog(null, "Erro: "+erro);
        }
    }

    public void sqlSelect(String sql)
    {
        try
        {
            statement = conexao.createStatement(resultset.TYPE_SCROLL_SENSITIVE, resultset.CONCUR_READ_ONLY);
            resultset = statement.executeQuery(sql);
        }
        catch (SQLException erro)
        {
            JOptionPane.showMessageDialog(null, "Erro: "+erro);
        }

    }
    
    public void executeSQL(String sql)
    {
        try
        {
            statement = conexao.createStatement(resultset.TYPE_SCROLL_SENSITIVE, resultset.CONCUR_READ_ONLY);
            status = statement.execute(sql);
        }
        catch (SQLException erro)
        {
            JOptionPane.showMessageDialog(null, "Erro: "+erro);
        }

    }
}